import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
    actions: {
        setToken(token) {
            this.token = token
            localStorage.setItem("token", token)
        },
        setIsLogin(value){
            this.isLogin = value
        },
        setInfo(key, value) {
            if (key === 'remove') {
                this.info = {
                    identity:"",
                    isTeacher:false
                }
                return
            }
            this.info[key] = value;
            if (key === 'identity' && value === 'teacher') {
                this.info.isTeacher = true
            }
        },

    },


    state: () => ({
        token: "",
        info:{
            identity:"",
            isTeacher:false
        },
        isLogin: false
    }),

    getters: {
        getToken(state) {
            if (state.token) {
                return state.token;
            }
            let token = localStorage.getItem("token")
            state.token = token
            return token
        },
    },
})
